#include "signal_4g.h"
#include "qpainter.h"
#include "qtimer.h"
#include "qdebug.h"
#include <QTimer>

Signal_4G::Signal_4G(QWidget *parent) : QWidget(parent)
{
    IsReStart=false;
    m_Count=5;
    m_CurrentValue=0;
    m_MinValue=0;
    m_MaxValue=100;
    m_Timer=new QTimer(this);
    connect(m_Timer,&QTimer::timeout,this,&Signal_4G::slots_TimeOut);
}

double Signal_4G::getMinValue() const
{
    return m_MinValue;
}

double Signal_4G::getMaxValue() const
{
    return m_MaxValue;
}

void Signal_4G::reStart(bool IsOpen)
{
    if(IsReStart==IsOpen){ return; }
    if(IsOpen==true){
        m_Timer->start(200);
        IsReStart=true;
    }else{
        m_Timer->stop();
        IsReStart=false;
        setCurrentValue(0);
    }
}

void Signal_4G::setMinValue(double minValue)
{
    m_MinValue=minValue;
}

void Signal_4G::setMaxValue(double maxValue)
{
    m_MaxValue=maxValue;
}

void Signal_4G::setCurrentValue(double currentValue)
{
    if(IsReStart==true){
        IsReStart=false;
        m_Timer->stop();
    }
    if(currentValue>90){ currentValue=0; }
    m_CurrentValue=currentValue;
    update();
}

void Signal_4G::slots_TimeOut()
{
    m_CurrentValue+=3;
    if(m_CurrentValue>=40){
        m_CurrentValue=0;
    }
    update();
}

void Signal_4G::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    int margin=10;
    double Width = width()-margin;
    double Height = height();

    double drawWidth=(Width*0.5)/m_Count;
    double Space=(Width-Width*0.5)/(m_Count-1);
    double diffheight=(Height*0.88)/m_Count;
    double begy=0;
    double begx=0;

    QColor Color_1(255, 255, 255);
    QColor Color_2(100,100,100);

    int splitValue=0;
    if(m_CurrentValue<11)
    {
        splitValue=-1;
    }else if(m_CurrentValue>=11&&m_CurrentValue<14)
    {
        splitValue=0;
    }else if(m_CurrentValue>=14&&m_CurrentValue<17)
    {
        splitValue=1;
    }else if(m_CurrentValue>=17&&m_CurrentValue<20)
    {
        splitValue=2;
    }else if(m_CurrentValue>=20&&m_CurrentValue<23)
    {
        splitValue=3;
    }else if(m_CurrentValue>=23)
    {
        splitValue=4;
    }
    //qDebug()<<"=="<<splitValue;
    for(int i=0;i<m_Count;i++){
        if(i<=splitValue&&splitValue!=-1){
            painter.setBrush(Color_1);
            painter.setPen(Color_1);
        }else{
            painter.setBrush(Color_2);
            painter.setPen(Color_2);
        }

        begy=diffheight*(m_Count-i-1);
        begx=drawWidth*i+Space*i;
        QRectF rectf(begx+margin/2,begy,drawWidth,Height-begy);
        painter.drawRoundedRect(rectf,2,2);
    }
}
